package com.paishen.peiniwan.module.login;

import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.widget.ImageView;

import com.paishen.common.util.BaseUtil;
import com.paishen.common.widget.RevealLayout;
import com.paishen.peiniwan.BaseActivity;
import com.paishen.peiniwan.R;


public class AmazingLRActivity extends BaseActivity {
    public static final String EXTRA_MODE_LOGIN = "extra_mode_login";

    private RevealLayout mRevealLayout;
    private LoginLayout layLogin;
    private ImageView imgRegister, imgRegBack;
    private RegisterLayout layRegister;
    private Animation.AnimationListener revealCallback;
    private boolean animSwitching;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isSupportTouchBack = false;
        setContentView(R.layout.activity_amazing_lr);

        BaseUtil.setStatusBarDarkMode(false, AmazingLRActivity.this);

        mRevealLayout = (RevealLayout) findViewById(R.id.reveal_layout);
        revealCallback = new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                animSwitching = true;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                animSwitching = false;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        };


        layLogin = (LoginLayout) findViewById(R.id.lay_login);
        if (getIntent().getBooleanExtra(EXTRA_MODE_LOGIN, false)) {
            layLogin.intoLoginMode();
        }


        imgRegister = layLogin.setOnRegisterClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int[] location = new int[2];
                v.getLocationOnScreen(location);
                int originalWidth = v.getWidth();
                int originalHeight = v.getHeight();
                int originalX = location[0] + originalWidth / 2;
                int originalY = location[1] + originalHeight / 2;
                mRevealLayout.next(originalX, originalY, revealCallback);
                imgRegister.setEnabled(false);
                imgRegBack.setEnabled(true);
                BaseUtil.hideSoftInputFromWindowAlways(AmazingLRActivity.this);
                BaseUtil.setStatusBarDarkMode(true, AmazingLRActivity.this);
            }
        });

        layRegister = (RegisterLayout) findViewById(R.id.lay_register);
        imgRegBack = layRegister.setOnRegisterBackClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int[] location = new int[2];
                v.getLocationOnScreen(location);
                int originalWidth = v.getWidth();
                int originalHeight = v.getHeight();
                int originalX = location[0] + originalWidth / 2;
                int originalY = location[1] + originalHeight / 2;
                mRevealLayout.next(originalX, originalY, revealCallback);
                imgRegister.setEnabled(true);
                imgRegBack.setEnabled(false);
                BaseUtil.hideSoftInputFromWindowAlways(AmazingLRActivity.this);
                BaseUtil.setStatusBarDarkMode(false, AmazingLRActivity.this);
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (animSwitching) {
            return;
        }

        if (!layLogin.dealBackPressed() && !layRegister.dealBackPressed()) {
            if (imgRegBack.isEnabled()) {
                WindowManager wm = (WindowManager) (getSystemService(Context.WINDOW_SERVICE));
                DisplayMetrics dm = new DisplayMetrics();
                wm.getDefaultDisplay().getMetrics(dm);
                mRevealLayout.next(dm.widthPixels, dm.heightPixels);
                imgRegister.setEnabled(true);
                imgRegBack.setEnabled(false);
                BaseUtil.setStatusBarDarkMode(false, AmazingLRActivity.this);
            } else {
                super.onBackPressed();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseUtil.hideSoftInputFromWindowAlways(AmazingLRActivity.this);
    }

    @Override
    public void onPause() {
        super.onPause();
        layLogin.getAmazingBgView().pause();
    }

    @Override
    public void onResume() {
        super.onResume();
        layLogin.getAmazingBgView().resume();
    }
}
